package exam.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import exam.common.util.ForwardResult;
import exam.db.dao.TUserDao;
import exam.db.entity.TUserEntity;
import exam.web.form.UserForm;

/**
 * <p>InsertUserAction</p>
 * 
 * @author PHONGTN
 * @version 19/4/2012
 */
public class InsertUserAction extends Action {
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// retForward
		String retForward = null;
		// UserForm
		UserForm userForm = (UserForm) form;
		
		// 
		TUserDao tUserDao = new TUserDao();
		HttpSession session = request.getSession();
		if (request.getParameter("add_user") != null) {
			// TUserEntity
			TUserEntity userEntity = new TUserEntity();
			// userEntity = userForm
			BeanUtils.copyProperties(userEntity, userForm);
			int rs = tUserDao.insertByFile(userEntity);
			if (rs == 0) {
				session.setAttribute("message", "Thêm mới người dùng thất bại!" + rs);
				retForward = ForwardResult.FAILED;
			} else {
				session.setAttribute("message", "Thêm mới người dùng thành công!" + rs);
				retForward = ForwardResult.SUCCESS;
			}
		} else {
			retForward = ForwardResult.FAILED;
		}
		// return success
		return mapping.findForward(retForward);
	}
}
